﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Core.Dao;
using Core.Entity;
using System.Data;

namespace Core.Business
{
    public class CityBus
    {
        private CityDao dao;
        public CityBus() {
            dao = new CityDao();
        }

        public bool insert(City city) {
            return dao.insert(city);
        }

        public bool update(City city) {
            return dao.update(city);
        }

        public bool delete(int id) {
            return dao.delete(id);
        }

        public bool delete(String ids) {
            return dao.delete(ids);
        }

        public City get(int id) {
            DataTable dt = dao.get(id);
            City city = new City();

            if (dt.Rows.Count > 0)
            {
                city.Id = Convert.ToInt32(dt.Rows[0]["CTY_ID"]);
                city.Name = dt.Rows[0]["CTY_NAME"].ToString();
                city.Desc = dt.Rows[0]["CTY_DESC"].ToString();
                city.CountryId = Convert.ToInt32(dt.Rows[0]["CNT_ID"]);
            }
            return city;
        }

        public List<City> getAll(int countryId) {
            DataTable dt = dao.getAll(countryId);
            List<City> list = new List<City>();

            if (dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    City city = new City();
                    city.Id = Convert.ToInt32(dr["CTY_ID"]);
                    city.Name = dr["CTY_NAME"].ToString();
                    city.Desc = dr["CTY_DESC"].ToString();
                    city.CountryId = Convert.ToInt32(dr["CNT_ID"]);
                    list.Add(city);
                }

            }
            return list;
        }
    }
}
